package com.gss.armory.util;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;

import org.apache.log4j.Logger;

import com.gss.common.util.ApplicationConstants;
import com.gss.common.util.ApplicationUtilities;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.exceptions.BadPasswordException;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;

public class DocAccessUtility {
	
	public static byte[] encryptDocBytes(byte[] docBytes,String openPassword,String editPassword){
		System.out.println("inside encrypt doc type..");
		PdfReader pdfReader = null;
		PdfStamper pdfStamper = null;
		
		try {
			try{
				pdfReader = new PdfReader(docBytes);
			}
			catch(BadPasswordException badPasswordException)
			{
				
				pdfReader = new PdfReader(docBytes,ApplicationConstants.EDIT_PWD.getBytes());
			}
			HashMap<String, String> docInfo = pdfReader.getInfo();
			docInfo.put("Title","Secured Document");
			docInfo.put("Author",ApplicationUtilities.getCurrentUser()+"@Armory");
			try {
				ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
				pdfStamper = new PdfStamper(pdfReader, byteArrayOutputStream);
				pdfStamper.setEncryption(openPassword.getBytes(), editPassword.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
				pdfStamper.setMoreInfo(docInfo);
				pdfStamper.close();
				return byteArrayOutputStream.toByteArray();
			} catch (DocumentException e) {
				e.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			pdfStamper = null;
			pdfReader = null;
			
		}
		return null;
	}
	
	public static String generatePassword(String userName)
	{
		long randomNum = (long)(Math.random() * 10000);
		String pwdText = Long.toString(randomNum) + Long.toString(System.currentTimeMillis());
		return pwdText+userName;

	}
	
	public static byte[] addWaterMark(byte[] docBytes,String openPassword,String editPassword){
		try {
			PdfReader pdfReader = new PdfReader(docBytes,ApplicationConstants.EDIT_PWD.getBytes());
			System.out.println("Number of pages: "+pdfReader.getNumberOfPages());
			HashMap<String, String> docInfo = pdfReader.getInfo();
			docInfo.put("Title","Secured Document");
			docInfo.put("Author",ApplicationUtilities.getCurrentUser()+"@Armory");
			PdfReader templateReader = new PdfReader(ApplicationConstants.WATERMARK_TEMPLATE_BYTES);
			try {
				ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
				PdfStamper pdfStamper = new PdfStamper(pdfReader, byteArrayOutputStream);
				pdfStamper.setEncryption(openPassword.getBytes(), editPassword.getBytes(),0,PdfWriter.STANDARD_ENCRYPTION_128);
				pdfStamper.setMoreInfo(docInfo);
				PdfImportedPage pdfImportedPage = pdfStamper.getImportedPage(templateReader, 1);
				int numOfPages = pdfReader.getNumberOfPages();
				PdfContentByte background;
				for(int i=1;i<=numOfPages;i++){
//					background = pdfStamper.getUnderContent(i);
					background = pdfStamper.getOverContent(i);	//changed on 07 August 2012 10:45 am by ashish
					background.addTemplate(pdfImportedPage, 0, 0);
				}
				pdfStamper.close();
				return byteArrayOutputStream.toByteArray();
			} catch (DocumentException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("Done!!!!");
		return null;
	}
}